import { useAuth } from "@/store/authContext";
import { useUserInfo, getOrderStatus } from "@/store/userDataContext";
import { useEffect, useState } from "react";

type StatusShape = {
  orderStatus: {
    status: string;
    price: string;
  };
  orderNo: string[];
};

export default function useOrderStatusData() {
  const { dispatchAuthAction } = useAuth();
  const { userInfoState, dispatchUserDataAction } = useUserInfo();
  const [orderStatusData, setOrderStatusData] = useState<null | StatusShape>();

  useEffect(() => {
    if (userInfoState?.orderNos) {
      const getStatus = async () => {
        const statusData = await getOrderStatus(dispatchUserDataAction, {
          query: { orderNo: userInfoState?.orderNos },
          authDispatch: dispatchAuthAction,
        });
        setOrderStatusData({
          orderStatus: statusData,
          orderNo: userInfoState?.orderNos!,
        });
      };
      getStatus();
    }
  }, [userInfoState?.orderNos]);

  return [orderStatusData];
}
